package jiubeirobot.com.robotshop.ui.activity.shop;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import jiubeirobot.com.robotshop.R;
import jiubeirobot.com.robotshop.base.Activitys;
import jiubeirobot.com.robotshop.base.BaseActivity;
import jiubeirobot.com.robotshop.base.BaseBean;
import jiubeirobot.com.robotshop.data.constans.Constants;
import jiubeirobot.com.robotshop.data.net.APIManager;
import jiubeirobot.com.robotshop.data.net.Flags;
import jiubeirobot.com.robotshop.utils.DataUtil;
import jiubeirobot.com.robotshop.utils.L;
import jiubeirobot.com.robotshop.utils.TS;
import jiubeirobot.com.robotshop.utils.glide.GlideImageLoadImpl;

public class QRPayActivity extends BaseActivity {

    @BindView(R.id.tv_back_txt)
    TextView tvBackTxt;
    @BindView(R.id.iv_search_icon)
    ImageView ivSearchIcon;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.llyt_back)
    LinearLayout llytBack;
    @BindView(R.id.tv_pay_des)
    TextView tvPayDes;
    @BindView(R.id.iv_pay_icon)
    ImageView ivPayIcon;
    @BindView(R.id.iv_pay_qr)
    ImageView ivPayQr;

    private long millisInFuture = 1800000;//30分钟 = 1800000毫秒
    private long countDownInterval = 1000;
    private CountDownTimer countDownTime; //计时类

    private int payType;
    private String money;
    String username;
    String token;
    String orderId;

    @Override
    public int pageLayout() {
        return R.layout.activity_qrpay;
    }

    @Override
    public void initTitle() {

        ivSearchIcon.setVisibility(View.GONE);
        tvBackTxt.setVisibility(View.GONE);
        tvTitle.setText(getString(R.string.shopping_pay_type));
    }

    @Override
    protected boolean isOpenStatus() {
        return super.isOpenStatus();
    }

    @Override
    public void initView() {

        payType = getIntent().getIntExtra(Constants.INTENT_PAY_TYPE, -1);
        money = getIntent().getStringExtra(Constants.PAY_MONEY);
        L.i(payType);

        username = DataUtil.getInstance().getMobileNumber(this);


        GlideImageLoadImpl.getInstance().load(this, null, ivPayQr);
        ivPayIcon.setImageResource(R.mipmap.wechat_pay);
        tvPayDes.setText(getString(R.string.shopping_pay_scan_wechatpay));
        requestPayInfo();

        countDownTime = new CountDownTimer(millisInFuture, countDownInterval) {
            @Override
            public void onTick(long millisUntilFinished) {
                L.i("millisUntilFinished：" + millisUntilFinished);
            }

            @Override
            public void onFinish() {
                TS.showToCenter(getString(R.string.common_timeout_pay));
                Activitys.toShoplist(QRPayActivity.this);
                countDownTime.start();
            }
        };
    }

    /**
     * 定义请求数据的方法
     */
    private void requestPayInfo() {

        if (payType == Constants.ALIPAY_TYPE) {
            APIManager.getInstance().getAliPayInfo(this, "", "");
        } else if (payType == Constants.WECHATPAY_TYPE) {

            APIManager.getInstance().getWechatPayInfo(this, "", "");
        }
    }

    @Override
    public void onSuccess(BaseBean bean, int flag) {
        switch (flag) {

            //使用Glide来加载的微信支付的二维码图片
            default:
                break;
        }

        if (flag == Flags.FLAG_SGOP_PAY_WECHAT || flag == Flags.FLAG_SGOP_PAY_ALI) {
            countDownTime.start();
            APIManager.getInstance().getOrderState(this, username, orderId, token);
        }
        dismissHUD();
    }


    @OnClick({R.id.llyt_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llyt_back:
                finish();
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (countDownTime != null) {
            countDownTime.cancel();
        }
    }
}
